using AuSys.Database.Context;
using AuSys.Repos;
using AuSys.Services.App;
using AuSys.Services.App.Cors;
using AuSys.Services.App.Logging;
using AuSys.Utils.Startup;
using Serilog;

try
{
    var builder = WebApplication.CreateBuilder(args);
    builder.Configuration.AddCustomConfigProviders();
    var c = builder.Configuration;

    //添加serilog
    builder.Services.AddSerilog(c);
    //添加数据库
    builder.Services.AddDb(c);
    //添加应用级服务
    builder.Services.AddAppServices(c);
    //添加数据库操作服务
    builder.Services.AddRepoServices();

    var app = builder.Build();

    if(app.Environment.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseOpenApi();
        app.UseSwaggerUi();
    }
    app.UseConfiguredCors();
    app.UseFileServer();
    app.UseRouting();
    app.UseAuthorization();
    app.UseRequestLogging(c);
    app.MapControllers();

    Log.Information("AuSys启动成功=============================================");
    app.Run();
}
catch (Exception ex)
{
    if (ex is not HostAbortedException)
        Log.Error(ex, "AuSys启动失败=============================================");
    throw;
}
finally
{
    Log.CloseAndFlush();
}
